package com.flyingocean;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class fileProccessor {

	/**
	 * 将用正则处理过的对象信息存入本地文件中
	 * 
	 * @param p
	 */
	boolean ifSuccess;
	
	fileProccessor(String filename) {
		// 读取文件到信息对象
		PlayerInfo p = pullFromRawFile(filename);

		// 处理信息对象
		p = extractInfo(p);

		// 将对象信息存入本地文件
		ifSuccess = pushIntoFile(p);

	}
	private boolean pushIntoFile(PlayerInfo p) {
		try {
			FileOutputStream file_fos = new FileOutputStream("Result.txt", true);
			OutputStreamWriter file_osw = new OutputStreamWriter(file_fos, "UTF-8");
			PrintWriter file = new PrintWriter(file_osw);
			/*
			 * 集中判断,如果该条数据的某个数据项为"null"(String类型), 则舍弃该条数据
			 */
			// 集中判断--开始
				if(p.ast=="null"
					||	p.birthDate=="null"
					||	p.blk=="null"
					||	p.city=="null"
					||	p.college=="null"
					||	p.drb=="null"
					||	p.fg=="null"
					||	p.fga=="null"
					||	p.fgper=="null"
					||	p.ft=="null"
					||	p.fta=="null"
					||	p.ftper=="null"
					||	p.g=="null"
					||	p.gs=="null"
					||	p.height=="null"
					||	p.l=="null"
					||	p.mp=="null"
					||	p.name=="null"
					||	p.number=="null"
					||	p.orb=="null"
					||	p.pf=="null"
					||	p.position=="null"
					||	p.pts=="null"
					||	p.season=="null"
					||	p.stl=="null"
					||	p.threep=="null"
					||	p.threepa=="null"
					||	p.threepper=="null"
					||	p.tm=="null"
					||	p.tov=="null"
					||	p.trb=="null"
					||	p.w=="null"
					||	p.weight=="null") {
					
					file.close();
					file_osw.close();
					file_fos.close();
					return false;
				}
			//集中判断--结束
			
			
			if (p.name == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.write(p.name);
				file.write("\t");
			}

			if (p.birthDate == "null") {
				file.write("null");
				file.write("\t");
			} else {
				String[] s = p.birthDate.split("-");
				file.print((int) Double.parseDouble(s[0]));
				file.write("\t");
				file.print((int) Double.parseDouble(s[1]));
				file.write("\t");
				file.print((int) Double.parseDouble(s[2]));
				file.write("\t");
			}

			if (p.height == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.height));
				file.write("\t");
			}

			if (p.weight == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.write(p.weight);
				file.write("\t");
			}

			// file.write(p.number);
			// file.write("\t"); 球衣号码不要了

			if (p.position == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.write(p.position);
				file.write("\t");
			}

			if (p.college == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.write(p.college);
				file.write("\t");
			}

			if (p.city == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.write(p.city);
				file.write("\t");
			}

			// 比赛数据
			if (p.season == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.write(p.season);
				file.write("\t");
			}

			if (p.tm == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.write(p.tm);
				file.write("\t");
			}

			if (p.g == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.write(p.g);
				file.write("\t");
			}

			if (p.gs == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.write(p.gs);
				file.write("\t");
			}

			if (p.mp == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.mp));
				file.write("\t");
			}

			if (p.fgper == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.fgper));
				file.write("\t");
			}

			if (p.fg == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.fg));
				file.write("\t");
			}

			if (p.fga == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.fga));
				file.write("\t");
			}

			if (p.threepper == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.threepper));
				file.write("\t");
			}

			if (p.threep == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.threep));
				file.write("\t");
			}

			if (p.threepa == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.threepa));
				file.write("\t");
			}

			if (p.ftper == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.ftper));
				file.write("\t");
			}

			if (p.ft == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.ft));
				file.write("\t");
			}

			if (p.fta == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.fta));
				file.write("\t");
			}

			if (p.trb == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.trb));
				file.write("\t");
			}

			if (p.orb == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.orb));
				file.write("\t");
			}

			if (p.drb == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.drb));
				file.write("\t");
			}

			if (p.ast == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.ast));
				file.write("\t");
			}

			if (p.stl == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.stl));
				file.write("\t");
			}

			if (p.blk == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.blk));
				file.write("\t");
			}

			if (p.tov == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.tov));
				file.write("\t");
			}

			if (p.pf == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.pf));
				file.write("\t");
			}

			if (p.pts == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.print(Double.parseDouble(p.pts));
				file.write("\t");
			}

			if (p.w == "null") {
				file.write("null");
				file.write("\t");
			} else {
				file.write(p.w);
				file.write("\t");
			}

			if (p.l == "null") {
				file.write("null");
				file.write("\n");
			} else {
				file.write(p.l);
				file.write("\n");
			}
			file.close();
			file_osw.close();
			file_fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;

	}

	/**
	 * 用正则处理从data中读取的对象信息
	 * 
	 * @param p
	 */
	private PlayerInfo extractInfo(PlayerInfo p) {
		String[] pattern = new String[33];
		pattern[0] = "<div class=\"row\"><div class=\"column\">全　　名:</div>(.*)</div>";
		pattern[1] = "<div class=\"row\"><div class=\"column\">出生日期:</div>(.*)年(.*)月(.*)日</div>";
		pattern[2] = "<div class=\"row\"><div class=\"column\">身　　高:</div>(.*)米.*</div>";
		pattern[3] = "<div class=\"row\"><div class=\"column\">体　　重:</div>(.*)公斤.*</div>";
		pattern[4] = "<div class=\"row\"><div class=\"column\">球衣号码:</div>(.*)<a href=";
		pattern[5] = "<div class=\"row\"><div class=\"column\">位　　置:</div>(.*)</div>";
		pattern[6] = "<div class=\"row\"><div class=\"column\">大　　学:</div>(.*)</div>";
		pattern[7] = "<div class=\"row\"><div class=\"column\">出生城市:</div>(.*)</div>";
		pattern[8] = "<td class=\"normal season\" colspan=\"2\">(.*)年</td>";
		pattern[9] = "<td class=\"normal tm\">(.*)支</td>";
		pattern[10] = "<td class=\"normal g\">(.*)</td>";
		pattern[11] = "<td class=\"normal gs\">(.*)</td>";
		pattern[12] = "<td class=\"normal mp\">(.*)</td>";
		pattern[13] = "<td class=\"normal fgper\">(.*)%</td>";
		pattern[14] = "<td class=\"normal fg\">(.*)</td>";
		pattern[15] = "<td class=\"normal fga\">(.*)</td>";
		pattern[16] = "<td class=\"normal threepper\">(.*)%</td>";
		pattern[17] = "<td class=\"normal threep\">(.*)</td>";
		pattern[18] = "<td class=\"normal threepa\">(.*)</td>";
		pattern[19] = "<td class=\"normal ftper\">(.*)%</td>";
		pattern[20] = "<td class=\"normal ft\">(.*)</td>";
		pattern[21] = "<td class=\"normal fta\">(.*)</td>";
		pattern[22] = "<td class=\"normal trb\">(.*)</td>";
		pattern[23] = "<td class=\"normal orb\">(.*)</td>";
		pattern[24] = "<td class=\"normal drb\">(.*)</td>";
		pattern[25] = "<td class=\"normal ast\">(.*)</td>";
		pattern[26] = "<td class=\"normal stl\">(.*)</td>";
		pattern[27] = "<td class=\"normal blk\">(.*)</td>";
		pattern[28] = "<td class=\"normal tov\">(.*)</td>";
		pattern[29] = "<td class=\"normal pf\">(.*)</td>";
		pattern[30] = "<td class=\"normal pts\">(.*)</td>";
		pattern[31] = "<td class=\"normal w\">(.*)</td>";
		pattern[32] = "<td class=\"normal l\">(.*)</td>";

		Pattern r;
		Matcher m;

		for (int i = 0; i < 33; i++) {
			r = Pattern.compile(pattern[i]);
			m = r.matcher(p.getValueByIndex(i));
			boolean ifFind = m.find();
			if (ifFind && (i != 1)) {
				p.setValueByIndex(i, m.group(1));
			} else if (ifFind && (i == 1)) {
				String x = m.group(1) + "-" + m.group(2) + "-" + m.group(3);
				p.setValueByIndex(i, x);
			} else {
				p.setValueByIndex(i, "null");
			}
		}
		return p;

	}

	/**
	 * 将data文件中的信息读入信息类中
	 * 
	 * @param filename
	 * @return
	 */
	private PlayerInfo pullFromRawFile(String filename) {
		PlayerInfo p = new PlayerInfo();
		try {
			FileInputStream is = new FileInputStream(filename);
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);

			String line = br.readLine();
			while (line != null) {
				p.name = line;

				line = br.readLine();
				p.birthDate = line;

				line = br.readLine();
				p.height = line;

				line = br.readLine();
				p.weight = line;

				line = br.readLine();
				p.number = line;

				line = br.readLine();
				p.position = line;

				line = br.readLine();
				p.college = line;

				line = br.readLine();
				p.city = line;

				line = br.readLine();
				p.season = line;

				line = br.readLine();
				p.tm = line;

				line = br.readLine();
				p.g = line;

				line = br.readLine();
				p.gs = line;

				line = br.readLine();
				p.mp = line;

				line = br.readLine();
				p.fgper = line;

				line = br.readLine();
				p.fg = line;

				line = br.readLine();
				p.fga = line;

				line = br.readLine();
				p.threepper = line;

				line = br.readLine();
				p.threep = line;

				line = br.readLine();
				p.threepa = line;

				line = br.readLine();
				p.ftper = line;

				line = br.readLine();
				p.ft = line;

				line = br.readLine();
				p.fta = line;

				line = br.readLine();
				p.trb = line;

				line = br.readLine();
				p.orb = line;

				line = br.readLine();
				p.drb = line;

				line = br.readLine();
				p.ast = line;

				line = br.readLine();
				p.stl = line;

				line = br.readLine();
				p.blk = line;

				line = br.readLine();
				p.tov = line;

				line = br.readLine();
				p.pf = line;

				line = br.readLine();
				p.pts = line;

				line = br.readLine();
				p.w = line;

				line = br.readLine();
				p.l = line;

				line = br.readLine();
			}
			br.close();
			isr.close();
			is.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return p;
	}

}
